package com.zzyl.nursing.controller.member;

import com.zzyl.common.core.controller.BaseController;
import com.zzyl.common.core.domain.AjaxResult;
import com.zzyl.common.core.domain.R;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.common.utils.UserThreadLocal;
import com.zzyl.nursing.domain.CountByTime;
import com.zzyl.nursing.domain.PageResult;
import com.zzyl.nursing.domain.Reservation;
import com.zzyl.nursing.dto.ReservationPageDto;
import com.zzyl.nursing.service.IReservationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.method.P;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 预约信息Controller
 *
 * @author ruoyi
 */
@RestController
@RequestMapping("/member/reservation")
@Api(tags = "预约信息相关接口")
public class MemberReservationController extends BaseController {

    @Autowired
    private IReservationService reservationService;


    /**
     * 查询取消预约数量
     * @return
     */
    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public R<Integer> getCancelledReservationCount() {
        Long userId = UserThreadLocal.getUserId();
        System.out.println("------" + userId);
        Integer count = reservationService.getCancelledCount(userId);
        return R.ok(count);
    }

    /**
     * 新增预约
     */
    @PostMapping
    @ApiOperation("新增预约")
    public R addReservation(@RequestBody @ApiParam("预约信息") Reservation reservation) {
        reservationService.insertReservation(reservation);
        return R.ok();
    }

    /**
     * 分页查询预约
     */
    @GetMapping("/page")
    @ApiOperation("分页查询预约")
    public AjaxResult selectReservationPage(@ApiParam("预约分页查询条件") ReservationPageDto reservationPageDto){
        PageResult<Reservation> list = reservationService.selectReservationPage(reservationPageDto);
        AjaxResult success = success(list);
        success.put("code", 200);
        success.put("msg", "请求成功");
        return success;
    }

    /**
     * 取消预约
     */
    @PutMapping("/{id}/cancel")
    public AjaxResult  cancelReservation(@PathVariable Long id){
        reservationService.cancelReservation(id);
        return success("取消成功");
    }

    /**
     * 查询每个时间段剩余预约次数
     * 同一时间段只能有六位家属预约
     */
    @GetMapping("/countByTime")
    public R<List<CountByTime>> countByTime(Long time){
        return R.ok(reservationService.countByTime(time));
    }
}